package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.biz.CaiminfoBiz;
import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TCaiminfo;
import com.zmht.beike.po.TCmonthcontent;
import com.zmht.beike.po.TCmonthinfo;
import com.zmht.specedu.vo.Teacher;
/**
 * 月主题action
 * @author Administrator
 *
 */
public class CmonthinfoAction extends BaseAction {

	/* 
	 * 初始化月主题添加页面
	 * (non-Javadoc)
	 * @see org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward initMonthInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String areaID = request.getParameter("areaID");
		String targetId = request.getParameter("targetId");
		
		//将领域信息等传入
		CaiminfoBiz biz = this.getCaiminfoBiz();
		TCaiminfo caiminfo = biz.findById(new Integer(targetId));
		caiminfo.setAreaId(new Integer(areaID));
		request.getSession().setAttribute("caiminfo", caiminfo);
		
		
		return mapping.findForward("addYuezhuti");
	}
	/**
	 * 添加月主题内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addMonthInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		
		//String monthThemeName = request.getParameter("monthThemeName");
		TCaiminfo caiminfo = (TCaiminfo)request.getSession().getAttribute("caiminfo");
		this.saveMonthInfo(request, teacher.getTeacherId(), caiminfo);
		//request.setAttribute("monthThemeName", monthThemeName);
		//System.out.println("----------------------areaID---------"+caiminfo.getAreaId());
		//跳转到
		return new ActionForward("/beike/cmonthinfo.do?method=getMonthInfo");
	}
	private boolean saveMonthInfo(HttpServletRequest request,Integer teacherId,TCaiminfo caiminfo){
		for(int i=1;i<11;i++){
			List<TCmonthcontent> list = this.getCmonthContentBiz().findCmonthContent(request.getSession().getId(), teacherId, i, 0, 100);
			if(list!=null&&list.size()>0){
				//保存月主题信息
				TCmonthinfo cmonthinfo = new TCmonthinfo();
				cmonthinfo.setTeacherId(teacherId);
				cmonthinfo.setMonthThemeName(list.get(0).getMonthThemeName());		
				cmonthinfo.setCaimId(caiminfo.getId());//目标id
				cmonthinfo.setTerm(caiminfo.getTerm());//学期
				cmonthinfo.setCareaId(caiminfo.getAreaId());//领域
				cmonthinfo.setMonth(i);
			
				//如果 老师ID 目标Id 领域ID 月份已存在则不添加新记录
				String hql = "from TCmonthinfo t where t.teacherId="+teacherId+" and t.caimId="+caiminfo.getId()+" and t.careaId='"+caiminfo.getAreaId()+"' and t.month="+i;
				Integer monthInfoId = 0;
				List<TCmonthinfo> monthinfos = this.getCmonthinfoBiz().findByHQL(hql);
				if(null!=monthinfos&&monthinfos.size()>0){
					monthInfoId = monthinfos.get(0).getId();
				}else{
				//保存并返回月主题id
					monthInfoId = this.getCmonthinfoBiz().saveMonthinfo(cmonthinfo);
				}
				for(TCmonthcontent t:list){
					t.setCmonthId(monthInfoId);//月主题id	
					t.setCareaId(caiminfo.getAreaId());
					this.getCmonthContentBiz().updateCmontContent(t);
				}
			}
		}
		
		
		return false;
	}
	/**
	 * 获得月主题记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getMonthInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		//开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		if(null!=request.getParameter("pageSize")){
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		List<TCmonthinfo> list = this.getCmonthinfoBiz().getMonthinfo(teacher.getTeacherId(), (index-1)*pageSize, pageSize);
		
		int total = this.getCmonthinfoBiz().getMonthinfoTotal(teacher.getTeacherId());
		request.setAttribute("currPage", index); 
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("monthinfos", list);
		
		
		request.setAttribute("teacher", teacher);
		return mapping.findForward("list_monthinfo");
	}
	/**
	 * 初始化月主题编辑页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initMonthInfoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String targetId = request.getParameter("targetId");
		String month = request.getParameter("month");
		String areaId= request.getParameter("areaId");
		
		
		request.setAttribute("month", month);
		request.setAttribute("targetId",targetId);
		request.setAttribute("areaId",areaId);
		return new ActionForward("/jsp/mubiaozhiding/edit_Yuezhuti.jsp");
	}
	/**
	 * 编辑内容完成后跳转
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editMonthInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
	
		//跳转到
		return new ActionForward("/beike/cmonthinfo.do?method=getMonthInfo");
	}
}
